package com.wave.lib;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Wave.Zhang
 * @version 1.0
 * @data 15/6/1
 */
public class SwipeItemViewAuto extends SwipeItemView {

    View mLeftView;

    ViewGroup mRightView;

    View mFrontView;

    public SwipeItemViewAuto(Context context) {
        super(context);
    }

    public SwipeItemViewAuto(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed) {
            //int w = this.getDisplay().getWidth();
            mLeftView = getChildAt(0);// 获取滑动菜单的view
            mFrontView = getChildAt(1);// 获得主页view
            mRightView = (ViewGroup) getChildAt(2);// 获取滑动菜单的view

            // 相当于fill_parent
            //mFrontView.measure(getWidth(),getHeight());
            mFrontView.layout(0, 0, getWidth(), mFrontView.getMeasuredHeight());

            mLeftView.measure(mLeftView.getMeasuredWidth(), mFrontView.getMeasuredHeight());
            mLeftView.layout(-(mLeftView.getMeasuredWidth()), 0, 0, mFrontView.getMeasuredHeight());

            mRightView.measure(mRightView.getMeasuredWidth(), mFrontView.getMeasuredHeight());
            mRightView.layout(getWidth(), 0, getWidth() + mRightView.getMeasuredWidth(), mFrontView.getMeasuredHeight());
            int mRs = mRightView.getChildCount();
            for (int i = 0; i < mRs; i++) {
                if(mRightView.getChildAt(i) instanceof ViewGroup){
                    ViewGroup vc = (ViewGroup)mRightView.getChildAt(i);
                    vc.layout(vc.getLeft(), 0, vc.getRight(), mFrontView.getMeasuredHeight());
                    if(vc.getChildCount()>0){
                        for (int j=0;j<vc.getChildCount();j++){
                            View ccv = vc.getChildAt(j);
                            ccv.layout(ccv.getLeft(), (mFrontView.getMeasuredHeight()  - ccv.getMeasuredHeight())/2, ccv.getRight(), (mFrontView.getMeasuredHeight()  + ccv.getMeasuredHeight())/2);
                        }
                    }
                }else{
                    View vc = mRightView.getChildAt(i);
                    vc.layout(vc.getLeft(), 0, vc.getRight(), mFrontView.getMeasuredHeight());
                }
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        /*for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }*/
        if (count > 1) {
            getChildAt(1).measure(widthMeasureSpec, heightMeasureSpec);
            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), getChildAt(1).getMeasuredHeight());
        }
    }
}
